#pragma once

#include "OCMain/HttpClient.h"
#include "OCMain/FileUtil.h"
#include "micaps4.pb.h"

using namespace OCPP;

std::function <void(const String&)> httpLog = [](const String& message) {
    std::cout << message << "\n";
};



const String host = "http://10.104.234.11:8080/DataService?";

String generatUrl(String lastUrl)
{
    return host + lastUrl;
}

void httpDownloadMicapsFile()
{
	long lHTTPSCode;
	HttpClientEx httpClient(httpLog);
    String url = generatUrl("requestType=getFileList&directory=SATELLITE/FY2G/L1/IR1/EQUAL");
    String data;
    httpClient.GetText(url,data,lHTTPSCode);
    micaps4::m4list m4ListProto;
    m4ListProto.ParseFromString(data);
    std::cout << "errorcode:" << m4ListProto.errorcode() << "\n";
    std::cout << "errormessage:" << m4ListProto.errormessage() << "\n";

    StringVector fileNameList;
    for (auto s : m4ListProto.filelist())
    {
        fileNameList.push_back(s.filename());
    }
    std::sort(fileNameList.begin(), fileNameList.end());
    //for (auto s : fileNameList)
    //{
    //    std::cout << s << "\n";
    //}

    {
        String fileName = fileNameList[0];
        String url = generatUrl("requestType=getData&directory=SATELLITE/FY2G/L1/IR1/EQUAL&fileName=" + fileName);
        String data;
        httpClient.GetText(url, data, lHTTPSCode); 
        micaps4::m4file m4FileProto;
        m4FileProto.ParseFromString(data);
        const String& bytes = m4FileProto.buffer();
        std::ofstream ofs(Singleton(Environment).getTmpPath() + fileName, std::ios::binary);
        ofs.write(bytes.c_str(), bytes.length());
        ofs.close();
    }
}

void parseMicaps4Block()
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    StringVector fileNameList;

    {
        micaps4::m4list m4ListProto;
        std::ifstream ifs("list.block", std::ios::binary);
        m4ListProto.ParseFromIstream(&ifs);
        std::cout << "errorcode:" << m4ListProto.errorcode() << "\n";
        std::cout << "errormessage:" << m4ListProto.errormessage() << "\n";
        auto list = m4ListProto.filelist();
        for (auto s : list)
        {
            fileNameList.push_back(s.filename());
        }
    }
    std::sort(fileNameList.begin(), fileNameList.end());
    for (auto s : fileNameList)
    {
        std::cout << s << "\n";
    }

    {
        micaps4::m4file m4FileProto;
        std::ifstream ifs("file.block", std::ios::binary);
        m4FileProto.ParseFromIstream(&ifs);
        std::cout << "errorcode:" << m4FileProto.errorcode() << "\n";
        std::cout << "errormessage:" << m4FileProto.errormessage() << "\n";
        const String& bytes = m4FileProto.buffer();
        std::cout << "bytes:" << bytes.length() << "\n";
        //std::ofstream ofs("ANI_IR1_R04_20230203_0400_FY2G.AWX", std::ios::binary);
        //ofs.write(bytes.c_str(), bytes.length());
        //ofs.close();
    }

    google::protobuf::ShutdownProtobufLibrary();
}

void ftpDownloadFile()
{
    FtpClientEx ftpClient("hx","hx",httpLog);
    String url = "ftp.data.bepk.cn/CAWN/G.11.1.1/2023-02-12/";
    
    String savePath = Singleton(Environment).getTmpPath();


    StringVector fileList = ftpClient.getFileList(url);

    for (auto fileName : fileList)
    {
        if (StringUtil::endsWith(fileName, "Dbz.zip")) 
        {
            ftpClient.DownloadToPath(url + fileName, savePath);
        }
    }
}